package it.koutifaris.travianmap.info;

import static it.koutifaris.testforclassutils.IsTestForAssert.assertIsTestFor;
import it.koutifaris.travianmap.info.TroopsLoader.OnTroopHandler;
import it.koutifaris.travianmap.model.Tribe;

import java.util.HashSet;

import junit.framework.Assert;
import android.test.AndroidTestCase;

public class TroopsLoaderTest extends AndroidTestCase {

	private TroopsLoader troopsLoader;

	@Override
	protected void setUp() throws Exception {
		troopsLoader = new TroopsLoader();
	}

	public void testPkgAndName() {
		assertIsTestFor(TroopsLoader.class);
	}

	public void testLoadAllData() {
		TestOnTroopHandler testOnTroopHandler = new TestOnTroopHandler();
		troopsLoader.load(testOnTroopHandler);

		int numTroopsPerTribe = 10;
		int numTribes = Tribe.values().length;
		int numExpectedTroops = numTribes * numTroopsPerTribe;

		Assert.assertEquals(numExpectedTroops, testOnTroopHandler.count);
	}

	public void testAllTribesLoaded() {
		TestOnTroopHandler testOnTroopHandler = new TestOnTroopHandler();
		troopsLoader.load(testOnTroopHandler);

		Tribe[] values = Tribe.values();
		for (Tribe tribe : values) {
			Assert.assertTrue(tribe + " is missing", testOnTroopHandler.tribes.contains(tribe));
		}

	}

	private static class TestOnTroopHandler implements OnTroopHandler {

		private int count = 0;
		private final HashSet<Tribe> tribes = new HashSet<Tribe>();

		@Override
		public void onTroop(Troop troop) {
			count++;
			tribes.add(troop.tribe());
		}

	}
}
